package cc.cix.hq.common.core.advice;

import cc.cix.hq.common.core.entity.enums.DbIndexType;
import cc.cix.hq.common.core.exception.HqException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class AopAdvice {

    @Pointcut("execution (* cc.cix.hq.*.service.*.*(..))")
    public void test() {

    }

    @Around("test()")
    public void aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) {
        try {
            proceedingJoinPoint.proceed();
        } catch (DuplicateKeyException e) {
            if (DbIndexType.isContains(e.getMessage())) {
                throw new HqException(DbIndexType.getDescByValue(e.getMessage()));
            }
        } catch (Throwable e) {
            throw new HqException(e.getMessage());
        }
    }

}